# -*- coding: utf-8 -*-
import sqlite3

class Settings():
    settings = {}
    settings["tables"] = [ 
                          [ "Widerstände", 
                           [        "UID",  "Wert",  "Material", "Leistung", "Bauform",  "Menge",    "Preis",        "Reichelt", "Notizen"],
                           {
                            "SQL": ["INTEGER PRIMARY KEY NOT NULL UNIQUE", "REAL", "INT", "REAL", "INT", "INT", "REAL", "TEXT", "TEXT"],
                            "GUI": ["int",  "real",  "combo",    "real",     "combo",    "spin",     "doublespin",   "text",      "text"],
                            "COMBO": {
                                      "Material":   ["Kohleschicht", "Metalloxid"],
                                      "Bauform":    ["DIL", "SMD"]
                                     }
                           } 
                          ]
#                          [ u"Kondensatoren",
#                           ["UID", u"Kapazität", "Einheit", "Typ", "Spannungsfestigkeit", u"Rastermaß", "Bauform", "Menge", "Preis", "Reichelt", "Notizen"],
#                           [1, 3, 4, 5, 6, 7, 8, 9],
#                           {"UID": "INTEGER PRIMARY KEY NOT NULL UNIQUE", u"Kapazität": "REAL", "Einheit": "TEXT", "Typ": "TEXT", "Spannungsfestigkeit": "INT", u"Rastermaß": "REAL", "Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
#                          ],
#                          [ "Dioden", 
#                           ["UID", "Bezeichnung", "Typ", "Spannung", u"Stromstärke", "Bauform", "Menge", "Preis", "Reichelt","Notizen"], 
#                           [2,8,9],
#                           {"UID": "INTEGER PRIMARY KEY  NOT NULL  UNIQUE" , "Bezeichnung": "TEXT", "Typ": "TEXT", "Spannung": "REAL", u"Stromstärke": "TEXT", "Bauform": "TEXT", "Menge": "INTEGER DEFAULT 0", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
#                          ],
#                          [ u"Transistoren",
#                           ["UID", "Typennummer", "Typ", "Spannungsfestigkeit", "Stromfestigkeit","Bauform", "Menge", "Preis", "Reichelt", "Notizen"],
#                           [1,2,3,4],
#                           {"UID": "INT", "Typennummer": "TEXT", "Typ": "TEXT", "Spannungsfestigkeit": "INT", "Stromfestigkeit": "INT","Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
#                          ],
#                          [ u"IC",
#                           ["UID", "Typennummer", "Typ", "Bauform" ,"Menge", "Preis", "Reichelt", "Notizen"],
#                           [1,2,3],
#                           {"UID":"INT", "Typennummer":"TEXT", "Typ":"TEXT", "Bauform":"TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
#                          ],
##                          [ u"",
##                           [,"Menge", "Preis", "Reichelt", "Notizen"],
##                           [],
##                           {, "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
##                          ],
#                          [ "Taktgeneratoren",
#                           ["UID", "Frequenz", "Einheit", "Typ", "Menge", "Preis", "Reichelt", "Notizen"],
#                           [1, 3, 6],
#                           {"UID": "INTEGER PRIMARY KEY  NOT NULL  UNIQUE", "Frequenz": "REAL", "Einheit": "TEXT", "Typ": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}                            
#                          ] 
                         ]
    def __init__(self):
        self.settingsConnection = sqlite3.connect("databases/settings.sqlite")
        self.settingsCursor = self.settingsConnection.cursor() 
        try:
            self.settingsCursor.execute('SELECT * FROM "main"."settings"')
            for row in self.settingsCursor:
                self.settings[row[0]] = row[1]         
        except Exception:
            try:
                self.settingsCursor.execute('CREATE TABLE "main"."settings" ("key" TEXT, "value" BLOB)')
            except:
                pass
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowHeight", "768")')
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowWidth", "1024")')  
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowPositionX", "0")')
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowPositionY", "0")')
            self.settingsConnection.commit()
            self.__init__()
            
    def __del__(self):
        self.settingsConnection.commit()
        self.settingsConnection.close()